﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;
using Kangaroo.Configuration.ConfigSaver;
using Kangaroo.Model;

namespace Kangaroo.Configuration
{
    public class Config
    {
        #region Static Accessor

        private static Config _instance;

        public static Config Instance
        {
            get
            {
                if (_instance == null)
                {
                    throw new InvalidOperationException("Trying to access Config before it is loaded.");
                }
                return _instance;
            }
        }

        #endregion

        #region Load & Save

        private static readonly IConfigSaver _configSaver = new XmlConfigSaver();

        public static Config Load(string filename)
        {
            return _instance = _configSaver.Load<Config>(filename);
        }

        public static Config LoadWithDecrypt(string filename)
        {
            return _instance = _configSaver.LoadWithDecrypt<Config>(filename);
        }

        public void Save(string filename)
        {
            _configSaver.Save(this, filename);
        }

        #endregion

        public Config()
        {
            ChannelPositions = new List<ChannelPosition>();
        }

        /// <summary>
        /// 变电站名称
        /// </summary>
        [DisplayName("变电站名称")]
        [XmlElement("变电站名称")]
        public string StationName { get; set; }

        /// <summary>
        /// 窗口标题
        /// </summary>
        [DisplayName("窗口标题")]
        [XmlElement("窗口标题")]
        public string WindowTitle { get; set; }

        /// <summary>
        /// 关闭密码
        /// </summary>
        [DisplayName("关闭密码")]
        [XmlElement("关闭密码")]
        public string Password { get; set; }

        /// <summary>
        /// 监控磁盘空间
        /// </summary>
        [XmlElement("监控磁盘空间")]
        public string MonitorVolumes { get; set; }

        /// <summary>
        /// 监控磁盘空间报警阙值
        /// </summary>
        [XmlElement("监控磁盘空间报警阙值")]
        public long MonitorVolumeSpaceQuota { get; set; }

        /// <summary>
        /// 设备图文件路径
        /// </summary>
        [XmlElement("设备图")]
        public string MapFile { get; set; }

        /// <summary>
        /// 设备名称设定
        /// </summary>
        [XmlArray("设备名称设定")]
        [XmlArrayItem("设备")]
        public List<EquipName> EquipNames { get; set; }

        public class EquipName
        {
            [XmlAttribute("设备ID")]
            public string EquipId { get; set; }

            [XmlAttribute("设备名称")]
            public string Name { get; set; }
        }

        /// <summary>
        /// 数据详情设定
        /// </summary>
        [XmlArray("数据详情设定")]
        [XmlArrayItem("显示项")]
        public List<DataDetailName> DataDetailNames { get; set; }

        public class DataDetailName
        {
            [XmlAttribute("ID")]
            public string Id { get; set; }

            [XmlAttribute("显示名称")]
            public string Name { get; set; }
        }

        /// <summary>
        /// 检测装置名称设定
        /// </summary>
        [XmlArray("检测装置名称设定")]
        [XmlArrayItem("装置")]
        public List<DeviceName> DeviceNames { get; set; }

        public class DeviceName
        {
            [XmlAttribute("装置ID")]
            public string DeviceId { get; set; }

            [XmlAttribute("装置名称")]
            public string Name { get; set; }
        }

        [XmlArray("检测位置设定")]
        [XmlArrayItem("位置")]
        public List<ChannelPosition> ChannelPositions { get; set; }

        public class ChannelPosition
        {
            [XmlElement("检测")]
            public ChannelInfo Channel { get; set; }

            [XmlAttribute("X")]
            public int X { get; set; }

            [XmlAttribute("Y")]
            public int Y { get; set; }
        }

        /// <summary>
        /// 报警等级设定
        /// </summary>
        [XmlArray("报警等级设定")]
        [XmlArrayItem("报警等级")]
        public List<AlarmLevelConfig> AlarmLevels { get; set; }

        public class AlarmLevelConfig
        {
            [XmlAttribute("等级")]
            public int AlarmLevel { get; set; }

            [XmlAttribute("颜色")]
            public int Color { get; set; }
        }

        [XmlElement("日志设定")]
        public LogConfig Log { get; set; }

        public class LogConfig
        {
            [XmlAttribute("日志文件目录")]
            public string LogFolder { get; set; }
        }

        [XmlElement("数据设定")]
        public DataConfig Data { get; set; }

        public class DataConfig
        {
            [XmlAttribute("数据库类型")]
            public string DatabaseType { get; set; }

            [XmlAttribute("连接字符串")]
            public string ConnectionString { get; set; }

            [XmlAttribute("刷新间隔秒")]
            public int UpdateIntervalInSeconds { get; set; }
        }
    }
}
